<?php
/**
 * 点大商城（www.diandashop.com） - 微信公众号小程序商城系统!
 * Copyright © 2020 山东点大网络科技有限公司 保留所有权利
 * =========================================================
 * 版本：V2
 * 授权主体：shop.guanjunbang.cn
 * 授权域名：shop.guanjunbang.cn
 * 授权码：vdcajlVLIVAgVdkfJvgY
 * ----------------------------------------------
 * 您只能在商业授权范围内使用，不可二次转售、分发、分享、传播
 * 任何企业和个人不得对代码以任何目的任何形式的再发布
 * =========================================================
 */

// +----------------------------------------------------------------------
// | 后台账号 子账号
// +----------------------------------------------------------------------
namespace app\controller;
use think\facade\View;
use think\facade\Db;

class WebUser extends Common
{
    public function initialize(){
		parent::initialize();
		$this->uid = session('BST_ID');
		$this->user = db('admin_user')->where(['id'=>$this->uid])->find();
		if(!session('BST_ID') || !$this->user || $this->user['isadmin'] != 2){
			showmsg('无访问权限');
		}
	}
	//账号列表
    public function index(){
		if(request()->isAjax()){
			$page = input('param.page');
			$limit = input('param.limit');
			
			if(input('param.field') && input('param.order')){
				if(input('param.field')=='logintime'){
					$order = 'admin_user.'.input('param.field').' '.input('param.order');
				}else{
					$order = 'admin.'.input('param.field').' '.input('param.order');
				}
			}else{
				$order = 'admin.id desc';
			}
			$where = [];
			if(input('param.aid')) $where[] = ['admin.id','=',input('param.aid')];
			if(input('param.un')) $where[] = ['admin_user.un','like','%'.input('param.un').'%'];
			if(input('param.tel')) $where[] = ['admin.tel','like','%'.input('param.tel').'%'];
			if(input('param.linkman')) $where[] = ['admin.linkman','like','%'.input('param.linkman').'%'];
			if(input('?param.status') && input('param.status')!=='') $where[] = ['admin.status','=',input('param.status')];
			$count = 0 + Db::name('admin')->alias('admin')->join('admin_user admin_user','admin.id=admin_user.aid and admin_user.isadmin>0 and admin_user.bid=0')->where($where)->count();
			//$lastsql = Db::getlastsql();
			$data = Db::name('admin')->alias('admin')->field('admin.*,admin_user.id uid,admin_user.un,admin_user.pwd,admin_user.logintime')->join('admin_user admin_user','admin.id=admin_user.aid and admin_user.bid=0 and admin_user.isadmin>0 and admin_user.bid=0')->where($where)->page($page,$limit)->order($order)->select()->toArray();
			//dump(Db::name()->getlastsql());
			//dump($data);die;
			foreach($data as $k=>$v){
				$data[$k]['wxappinfo'] = \app\common\System::appinfo($v['id'],'wx');
				$data[$k]['mpappinfo'] = \app\common\System::appinfo($v['id'],'mp');
				$admin_set = db('admin_set')->where('aid',$v['id'])->find();
				if(!$admin_set){
					$color1 = '#FD4A46';
				}else{
					$color1 = $admin_set['color1'];
				}
				$data[$k]['color1'] = $color1;
			}
			return ['code'=>0,'msg'=>'查询成功','count'=>$count,'data'=>$data];
		}
		if(input('param.type')==1){
			$lastDayStart = strtotime(date('Y-m-d',time()-86400));
			$lastDayEnd = $lastDayStart + 86400;
			$thisMonthStart = strtotime(date('Y-m-1'));
			$nowtime = time();
			//会员数
			$memberCount = 0 + Db::name('member')->where([])->count();
			$memberLastDayCount = 0 + Db::name('member')->where('createtime','>=',$lastDayStart)->where('createtime','<',$lastDayEnd)->count();
			$memberThisMonthCount = 0 + Db::name('member')->where('createtime','>=',$thisMonthStart)->where('createtime','<',$nowtime)->count();
			//订单数 订单金额
			$ordernumCount = 0 + Db::name('shop_order')->where('status','in','1,2,3')->count();
			$ordernumLastDayCount = 0 + Db::name('shop_order')->where('status','in','1,2,3')->where('paytime','>=',$lastDayStart)->where('paytime','<',$lastDayEnd)->count();
			$ordernumThisMonthCount = 0 + Db::name('shop_order')->where('status','in','1,2,3')->where('paytime','>=',$thisMonthStart)->where('paytime','<',$nowtime)->count();
			$ordermoneyCount = 0 + Db::name('shop_order')->where('status','in','1,2,3')->sum('totalprice');
			$ordermoneyLastDayCount = 0 + Db::name('shop_order')->where('status','in','1,2,3')->where('paytime','>=',$lastDayStart)->where('paytime','<',$lastDayEnd)->sum('totalprice');
			$ordermoneyThisMonthCount = 0 + Db::name('shop_order')->where('status','in','1,2,3')->where('paytime','>=',$thisMonthStart)->where('paytime','<',$nowtime)->sum('totalprice');
			//商品数量
			$productCount = 0 + Db::name('shop_product')->where([])->count();
			$product0Count = 0 + Db::name('shop_product')->where('status',0)->count();
			$product1Count = 0 + Db::name('shop_product')->where('status',1)->count();
			
			//收款金额
			$payCount = Db::name('payorder')->where('paytypeid','not in','1,4')->where('status',1)->sum('money');
			$payLastDayCount = 0 + Db::name('payorder')->where('paytypeid','not in','1,4')->where('status',1)->where('paytime','>=',$lastDayStart)->where('paytime','<',$lastDayEnd)->sum('money');
			$payThisMonthCount = 0 + Db::name('payorder')->where('paytypeid','not in','1,4')->where('status',1)->where('paytime','>=',$thisMonthStart)->where('paytime','<',$nowtime)->sum('money');
			
			View::assign('memberCount',$memberCount);
			View::assign('memberLastDayCount',$memberLastDayCount);
			View::assign('memberThisMonthCount',$memberThisMonthCount);
			View::assign('ordernumCount',$ordernumCount);
			View::assign('ordernumLastDayCount',$ordernumLastDayCount);
			View::assign('ordernumThisMonthCount',$ordernumThisMonthCount);
			View::assign('ordermoneyCount',$ordermoneyCount);
			View::assign('ordermoneyLastDayCount',$ordermoneyLastDayCount);
			View::assign('ordermoneyThisMonthCount',$ordermoneyThisMonthCount);
			View::assign('productCount',$productCount);
			View::assign('product0Count',$product0Count);
			View::assign('product1Count',$product1Count);
			View::assign('payCount',$payCount);
			View::assign('payLastDayCount',$payLastDayCount);
			View::assign('payThisMonthCount',$payThisMonthCount);

			//是否配置了计划任务
			$autotimes = cache('autotimes');
			if($autotimes){
				$isauto = 1;
			}else{
				$isauto = 0;
			}
			View::assign('isauto',$isauto);

			$config = include('config.php');
			$authkey = $config['authkey'];
			$domain = $_SERVER['HTTP_HOST'];
			$rs = request_post('https://www.diandashop.com/index/upgrade2/getversion',['authkey'=>$authkey,'authdomain'=>$domain]);
			$rsdata = json_decode($rs,true);
			$needupgrade = 0;
			$myversion = file_get_contents('version.php');
			if($rsdata && $rsdata['status']==1 &&  $rsdata['version'] != $myversion){
				$needupgrade = 1;
			}
			View::assign('needupgrade',$needupgrade);
			View::assign('myversion',$myversion);
			View::assign('newversion',$rsdata['version']);

		}
		return View::fetch();
    }
	//登录
	public function alogin(){
		$rs = Db::name('admin_user')->where(array('id'=>input('param.uid/d')))->find();
		if($rs){
			session('ADMIN_LOGIN',1);
			session('ADMIN_UID',$rs['id']);
			session('ADMIN_AID',$rs['aid']);
			session('ADMIN_BID',$rs['bid']);
			session('ADMIN_NAME',$rs['un']);
			session('IS_ADMIN',$rs['isadmin']);
			return redirect((string)url('Backstage/index'));
		}
	}
	//编辑
	public function edit(){
		$menudata = \app\common\Menu::getdata(0,0);
		if(input('param.id')){
			$id = input('param.id/d');
			define('aid',$id);
			$ainfo = Db::name('admin')->where('id',$id)->find();
			$asetinfo = Db::name('admin_set')->where('aid',$id)->find();
			$uinfo = Db::name('admin_user')->where('aid',$id)->where('bid',0)->where('isadmin','>',0)->find();
			$auth_data = $uinfo ? json_decode($uinfo['auth_data'],true) : array();
		}else{
			$ainfo = ['type'=>1,'status'=>1,'endtime'=>time()+86400*365,'platform'=>'mp,wx'];
			$asetinfo = [];
			$uinfo = [];
			$auth_data = [];
		}

        $wxauth_data = $uinfo ? json_decode($uinfo['wxauth_data'],true) : array();
        if(!$wxauth_data) $wxauth_data = array();
        $notice_auth_data = $uinfo ? json_decode($uinfo['notice_auth_data'],true) : array();
        if(!$notice_auth_data) $notice_auth_data = array();
        $hexiao_auth_data = $uinfo ? json_decode($uinfo['hexiao_auth_data'],true) : array();
        if(!$hexiao_auth_data) $hexiao_auth_data = array();
        $wxauth_data = $uinfo ? json_decode($uinfo['wxauth_data'],true) : array();
        if(!$wxauth_data) $wxauth_data = array();

		View::assign('auth_data',$auth_data);
        View::assign('notice_auth_data',$notice_auth_data);
        View::assign('hexiao_auth_data',$hexiao_auth_data);
        View::assign('wxauth_data',$wxauth_data);
		View::assign('menudata',$menudata);

		View::assign('ainfo',$ainfo);
		View::assign('asetinfo',$asetinfo);
		View::assign('uinfo',$uinfo);
		View::assign('rinfo',json_decode($ainfo['remote'],true));
		return View::fetch();
	}
	public function save(){
		$ainfo = input('post.ainfo/a');
		$uinfo = input('post.uinfo/a');
        $uinfo['notice_auth_data'] = jsonEncode(input('post.notice_auth_data/a'));
        $uinfo['hexiao_auth_data'] = jsonEncode(input('post.hexiao_auth_data/a'));
        $uinfo['wxauth_data'] = jsonEncode(input('post.wxauth_data/a'));
		$uinfo['auth_data'] = str_replace('^_^','\/*',jsonEncode(input('post.auth_data/a')));
		$ainfo['platform'] = implode(',',$ainfo['platform']);
		$ainfo['remote'] = jsonEncode(input('post.rinfo/a'));
		//$asetinfo = input('post.asetinfo/a');

		$hasun = Db::name('admin_user')->where('id','<>',$uinfo['id'])->where('un',$uinfo['un'])->find();
		if($hasun){
			return json(['status'=>0,'msg'=>'该账号已存在']);
		}
		$ainfo['endtime'] = strtotime($ainfo['endtime']);
		if($ainfo['id']){
			Db::name('admin')->where('id',$ainfo['id'])->update($ainfo);
			\app\common\System::plog('编辑用户'.$ainfo['id'],1);
		}else{
			$ainfo['createtime'] = time();
			$ainfo['token'] = random(10);
			$ainfo['id'] = Db::name('admin')->insertGetId($ainfo);
			\app\common\System::initaccount($ainfo['id']);
			\app\common\System::plog('添加用户'.$ainfo['id'],1);
		}
		if($uinfo['id']){
			if($uinfo['pwd']!=''){
				$uinfo['pwd'] = md5($uinfo['pwd']);
			}else{
				unset($uinfo['pwd']);
			}
			Db::name('admin_user')->where('id',$uinfo['id'])->update($uinfo);
		}else{
			$uinfo['pwd'] = md5($uinfo['pwd']);
			$uinfo['aid'] = $ainfo['id'];
			$uinfo['createtime'] = time();
			$uinfo['isadmin'] = 1;
			$uinfo['random_str'] = random(16);
			Db::name('admin_user')->insert($uinfo);
		}
		return json(['status'=>1,'msg'=>'操作成功','url'=>(string)url('index')]);
	}
	//删除
	public function del(){
		$ids = input('post.ids/a');
		foreach($ids as $id){
			if($id == 1) return json(['status'=>0,'msg'=>'总账号不允许删除']);
			$admin = Db::name('admin')->where('id',intval($id))->find();
			Db::name('admin')->where('id',intval($id))->delete();
			Db::name('admin_user')->where('aid',intval($id))->where('bid',0)->delete();
			Db::name('admin_set')->where('aid',intval($id))->delete();
			Db::name('admin_setapp_mp')->where('aid',intval($id))->delete();
			Db::name('admin_setapp_wx')->where('aid',intval($id))->delete();
			Db::name('admin_setapp_alipay')->where('aid',intval($id))->delete();
			Db::name('admin_setapp_app')->where('aid',intval($id))->delete();
			Db::name('admin_setapp_baidu')->where('aid',intval($id))->delete();
			Db::name('admin_setapp_h5')->where('aid',intval($id))->delete();
			Db::name('admin_setapp_qq')->where('aid',intval($id))->delete();
			Db::name('admin_setapp_toutiao')->where('aid',intval($id))->delete();
		}
		\app\common\System::plog('删除用户'.implode(',',$ids),1);
		return json(['status'=>1,'msg'=>'删除成功']);
	}

	//下载小程序uniapp代码包
	public function downloaduniapp(){
		if($_SERVER['HTTP_HOST'] == 'v2.diandashop.com' || $_SERVER['HTTP_HOST'] == 'v2a.diandashop.com'){
			return json(['status'=>0,'msg'=>'演示站无下载权限']);
		}
		$aid = input('post.aid');

		$navigationBarBackgroundColor = input('post.navigationBarBackgroundColor');
		$navigationBarTextStyle = input('post.navigationBarTextStyle');
		$admin = Db::name('admin')->where('id',$aid)->find();
		$sysset = Db::name('admin_set')->where('aid',$aid)->find();
		$appapp = Db::name('admin_setapp_app')->where('aid',$aid)->find();

        //import('file',EXTEND_PATH);
		$wxdir = ROOT_PATH.'uniapp';
		$copydir = ROOT_PATH.'upload/uniapp'.$aid;
		\app\common\File::clear_dir($copydir);
		\app\common\File::all_copy($wxdir,$copydir);

		//配置文件 pages.json
		$window = array(
			"navigationBarBackgroundColor"=>$navigationBarBackgroundColor,
			"navigationBarTextStyle"=>$navigationBarTextStyle,
			"navigationBarTitleText"=> '',
			"h5"=>["titleNView"=>false]
		);
		
		//页面 pages
		$pagesjson = file_get_contents(ROOT_PATH.'uniapp/pages.json');
		$pagesjson = str_replace('"navigationBarTextStyle": "black"','"navigationBarTextStyle": "'.$navigationBarTextStyle.'"',$pagesjson);
		$pagesjson = str_replace('"navigationBarBackgroundColor": "#F8F8F8"','"navigationBarBackgroundColor": "'.$navigationBarBackgroundColor.'"',$pagesjson);
		
		$pagesjson = str_replace('"navigationBarTextStyle":"black"','"navigationBarTextStyle":"'.$navigationBarTextStyle.'"',$pagesjson);
		$pagesjson = str_replace('"navigationBarBackgroundColor":"#F8F8F8"','"navigationBarBackgroundColor":"'.$navigationBarBackgroundColor.'"',$pagesjson);

		file_put_contents($copydir.'/pages.json',$pagesjson);
		
		//配置信息
		$uniacid = $aid;
		if($admin['domain']){
			$siteroot = 'https://'.$admin['domain'];
		}else{
			$siteroot = str_replace('http://','https://',request()->domain());
		}
		$siteinfostr = 'var siteinfo = {"uniacid":"'.$uniacid.'","siteroot":"'.$siteroot.'"};module.exports = siteinfo;';
		file_put_contents($copydir.'/siteinfo.js',$siteinfostr);
		
		$zipname = uniqid().'.zip';
		$zippath = ROOT_PATH.'upload/'.$zipname;
		$myfile = fopen($zippath, "w");
		fclose($myfile);
		\app\common\File::add_file_to_zip($copydir,$zippath,'uniapp_'.$aid);
		$url = PRE_URL.'/upload/'.$zipname;
		\app\common\File::remove_dir($copydir);
		\app\common\System::plog('下载uniapp代码'.$aid,1);
		return json(['status'=>1,'msg'=>'打包成功','url'=>$url]);
	}

	//复制数据
	public function copydata(){
		set_time_limit(0);
		ini_set('memory_limit','-1');

		$info = input('post.info/a');
		$fromid = $info['fromid'];
		$toid = $info['toid'];
		$delold = $info['delold'];
		$module_data = input('post.module_data');
		if(!$module_data)  return json(['status'=>0,'msg'=>'请选择要复制的数据']);
		$fromadmin = Db::name('admin')->where('id',$fromid)->find();
		if(!$fromadmin) return json(['status'=>0,'msg'=>'来源账号未查找到']);
		$toadmin = Db::name('admin')->where('id',$toid)->find();
		if(!$toadmin) return json(['status'=>0,'msg'=>'要复制到的账号未查找到']);
		
		foreach($module_data as $modulename){
			if($modulename == '商城商品'){
				//分类
				if($delold == 1) Db::name('shop_category')->where('aid',$toid)->delete();
				$fromdata = Db::name('shop_category')->where('aid',$fromid)->select()->toArray();
				$shop_category_ids_map = [];
				$shop_category_ids_map['0'] = '0';
				foreach($fromdata as $data){
					$oldid = $data['id'];
					$data['id'] = '';
					$data['aid'] = $toid;
					$data['pid'] = $shop_category_ids_map[$data['pid']];
					$shop_category_ids_map[$oldid] = Db::name('shop_category')->insertGetId($data);
				}
				//分组
				if($delold == 1) Db::name('shop_group')->where('aid',$toid)->delete();
				$fromdata = Db::name('shop_group')->where('aid',$fromid)->select()->toArray();
				$shop_group_ids_map = [];
				$shop_group_ids_map['0'] = '0';
				foreach($fromdata as $data){
					$oldid = $data['id'];
					$data['id'] = '';
					$data['aid'] = $toid;
					$shop_group_ids_map[$oldid] = Db::name('shop_group')->insertGetId($data);
				}
				//服务
				if($delold == 1) Db::name('shop_fuwu')->where('aid',$toid)->where('bid',0)->delete();
				$fromdata = Db::name('shop_fuwu')->where('aid',$fromid)->where('bid',0)->select()->toArray();
				$shop_fuwu_ids_map = [];
				$shop_fuwu_ids_map['0'] = '0';
				foreach($fromdata as $data){
					$oldid = $data['id'];
					$data['id'] = '';
					$data['aid'] = $toid;
					$shop_fuwu_ids_map[$oldid] = Db::name('shop_fuwu')->insertGetId($data);
				}
				//参数
				if($delold == 1) Db::name('shop_param')->where('aid',$toid)->delete();
				$fromdata = Db::name('shop_param')->where('aid',$fromid)->where('bid',0)->select()->toArray();
				foreach($fromdata as $data){
					$oldid = $data['id'];
					$data['id'] = '';
					$data['aid'] = $toid;
					$data['cid'] = $this->getnewids($shop_category_ids_map,$data['cid']);
					Db::name('shop_param')->insertGetId($data);
				}
				//商品
				if($delold == 1){
					$proids = Db::name('shop_product')->where('aid',$toid)->where('bid',0)->column('id');
					Db::name('shop_product')->where('aid',$toid)->where('bid',0)->delete();
					Db::name('shop_guige')->where('aid',$toid)->where('proid','in',$proids)->delete();
				}
				$fromdata = Db::name('shop_product')->where('aid',$fromid)->where('bid',0)->select()->toArray();
				foreach($fromdata as $data){
					$oldid = $data['id'];
					unset($data['wxvideo_product_id']);
					unset($data['wxvideo_edit_status']);
					unset($data['wxvideo_status']);
					unset($data['wxvideo_reject_reason']);
					$data['id'] = '';
					$data['aid'] = $toid;
					$data['cid'] = $this->getnewids($shop_category_ids_map,$data['cid']);
					$data['gid'] = $this->getnewids($shop_group_ids_map,$data['gid']);
					$data['fwid'] = $this->getnewids($shop_fuwu_ids_map,$data['fwid']);
					if($data['freighttype'] == 0) $data['freighttype'] = 1;
					//if($data['commissionset'] == 1 || $data['commissionset'] == 2 || $data['commissionset'] == 3) $data['commissionset'] = 0;
					$id = Db::name('shop_product')->insertGetId($data);
					$gglist = Db::name('shop_guige')->where('aid',$fromid)->where('proid',$oldid)->select()->toArray();
					foreach($gglist as $gg){
						$gg['id'] = '';
						$gg['aid'] = $toid;
						$gg['proid'] = $id;
						Db::name('shop_guige')->insert($gg);
					}
				}
			}
			if($modulename == '拼团商品'){
				//分类
				if($delold == 1) Db::name('collage_category')->where('aid',$toid)->delete();
				$fromdata = Db::name('collage_category')->where('aid',$fromid)->select()->toArray();
				$collage_category_ids_map = [];
				$collage_category_ids_map['0'] = '0';
				foreach($fromdata as $data){
					$oldid = $data['id'];
					$data['id'] = '';
					$data['aid'] = $toid;
					$data['pid'] = $collage_category_ids_map[$data['pid']];
					$collage_category_ids_map[$oldid] = Db::name('collage_category')->insertGetId($data);
				}
				//商品
				if($delold == 1){
					$proids = Db::name('collage_product')->where('aid',$toid)->where('bid',0)->column('id');
					Db::name('collage_product')->where('aid',$toid)->where('bid',0)->delete();
					Db::name('collage_guige')->where('aid',$toid)->where('proid','in',$proids)->delete();
				}
				$fromdata = Db::name('collage_product')->where('aid',$fromid)->where('bid',0)->select()->toArray();
				foreach($fromdata as $data){
					$oldid = $data['id'];
					$data['id'] = '';
					$data['aid'] = $toid;
					$data['cid'] = $this->getnewids($collage_category_ids_map,$data['cid']);
					if($data['freighttype'] == 0) $data['freighttype'] = 1;
					//if($data['commissionset'] == 1 || $data['commissionset'] == 2 || $data['commissionset'] == 3) $data['commissionset'] = 0;
					$id = Db::name('collage_product')->insertGetId($data);
					$gglist = Db::name('collage_guige')->where('aid',$fromid)->where('proid',$oldid)->select()->toArray();
					foreach($gglist as $gg){
						$gg['id'] = '';
						$gg['aid'] = $toid;
						$gg['proid'] = $id;
						Db::name('collage_guige')->insert($gg);
					}
				}
			}
			if($modulename == '砍价商品'){
				//商品
				if($delold == 1) Db::name('kanjia_product')->where('aid',$toid)->where('bid',0)->delete();
				$fromdata = Db::name('kanjia_product')->where('aid',$fromid)->where('bid',0)->select()->toArray();
				foreach($fromdata as $data){
					$oldid = $data['id'];
					$data['id'] = '';
					$data['aid'] = $toid;
					if($data['freighttype'] == 0) $data['freighttype'] = 1;
					$id = Db::name('kanjia_product')->insertGetId($data);
				}
			}
			if($modulename == '秒杀商品'){
				if($delold == 1){
					$proids = Db::name('seckill_product')->where('aid',$toid)->where('bid',0)->column('id');
					Db::name('seckill_product')->where('aid',$toid)->where('bid',0)->delete();
					Db::name('seckill_guige')->where('aid',$toid)->where('proid','in',$proids)->delete();
				}
				$fromdata = Db::name('seckill_product')->where('aid',$fromid)->where('bid',0)->select()->toArray();
				foreach($fromdata as $data){
					$oldid = $data['id'];
					$data['id'] = '';
					$data['aid'] = $toid;
					if($data['freighttype'] == 0) $data['freighttype'] = 1;
					//if($data['commissionset'] == 1 || $data['commissionset'] == 2 || $data['commissionset'] == 3) $data['commissionset'] = 0;
					$id = Db::name('seckill_product')->insertGetId($data);
					$gglist = Db::name('seckill_guige')->where('aid',$fromid)->where('proid',$oldid)->select()->toArray();
					foreach($gglist as $gg){
						$gg['id'] = '';
						$gg['aid'] = $toid;
						$gg['proid'] = $id;
						Db::name('seckill_guige')->insert($gg);
					}
				}
			}
			if($modulename == '团购商品'){
				//分类
				if($delold == 1) Db::name('tuangou_category')->where('aid',$toid)->where('bid',0)->delete();
				$fromdata = Db::name('tuangou_category')->where('aid',$fromid)->where('bid',0)->select()->toArray();
				$tuangou_category_ids_map = [];
				$tuangou_category_ids_map['0'] = '0';
				foreach($fromdata as $data){
					$oldid = $data['id'];
					$data['id'] = '';
					$data['aid'] = $toid;
					$data['pid'] = $tuangou_category_ids_map[$data['pid']];
					$tuangou_category_ids_map[$oldid] = Db::name('tuangou_category')->insertGetId($data);
				}
				//商品
				if($delold == 1) Db::name('tuangou_product')->where('aid',$toid)->where('bid',0)->delete();
				$fromdata = Db::name('tuangou_product')->where('aid',$fromid)->where('bid',0)->select()->toArray();
				foreach($fromdata as $data){
					$oldid = $data['id'];
					$data['id'] = '';
					$data['aid'] = $toid;
					$data['cid'] = $this->getnewids($tuangou_category_ids_map,$data['cid']);
					if($data['freighttype'] == 0) $data['freighttype'] = 1;
					//if($data['commissionset'] == 1 || $data['commissionset'] == 2 || $data['commissionset'] == 3) $data['commissionset'] = 0;
					$id = Db::name('tuangou_product')->insertGetId($data);
				}
			}
			if($modulename == '积分兑换商品'){
				//分类
				if($delold == 1) Db::name('scoreshop_category')->where('aid',$toid)->delete();
				$fromdata = Db::name('scoreshop_category')->where('aid',$fromid)->select()->toArray();
				$scoreshop_category_ids_map = [];
				$scoreshop_category_ids_map['0'] = '0';
				foreach($fromdata as $data){
					$oldid = $data['id'];
					$data['id'] = '';
					$data['aid'] = $toid;
					$data['pid'] = $scoreshop_category_ids_map[$data['pid']];
					$scoreshop_category_ids_map[$oldid] = Db::name('scoreshop_category')->insertGetId($data);
				}
				//商品
				if($delold == 1) Db::name('scoreshop_product')->where('aid',$toid)->delete();
				$fromdata = Db::name('scoreshop_product')->where('aid',$fromid)->select()->toArray();
				foreach($fromdata as $data){
					$oldid = $data['id'];
					$data['id'] = '';
					$data['aid'] = $toid;
					$data['cid'] = $this->getnewids($scoreshop_category_ids_map,$data['cid']);
					if($data['freighttype'] == 0) $data['freighttype'] = 1;
					//if($data['commissionset'] == 1 || $data['commissionset'] == 2 || $data['commissionset'] == 3) $data['commissionset'] = 0;
					$id = Db::name('scoreshop_product')->insertGetId($data);
				}
			}
			if($modulename == '幸运拼团商品'){
				//分类
				if($delold == 1) Db::name('lucky_collage_category')->where('aid',$toid)->delete();
				$fromdata = Db::name('lucky_collage_category')->where('aid',$fromid)->select()->toArray();
				$luckycollage_category_ids_map = [];
				$luckycollage_category_ids_map['0'] = '0';
				foreach($fromdata as $data){
					$oldid = $data['id'];
					$data['id'] = '';
					$data['aid'] = $toid;
					$data['pid'] = $luckycollage_category_ids_map[$data['pid']];
					$luckycollage_category_ids_map[$oldid] = Db::name('lucky_collage_category')->insertGetId($data);
				}
				//商品
				if($delold == 1){
					$proids = Db::name('lucky_collage_product')->where('aid',$toid)->where('bid',0)->column('id');
					Db::name('lucky_collage_product')->where('aid',$toid)->where('bid',0)->delete();
					Db::name('lucky_collage_guige')->where('aid',$toid)->where('proid','in',$proids)->delete();
				}
				$fromdata = Db::name('lucky_collage_product')->where('aid',$fromid)->where('bid',0)->select()->toArray();
				foreach($fromdata as $data){
					$oldid = $data['id'];
					$data['id'] = '';
					$data['aid'] = $toid;
					$data['cid'] = $this->getnewids($luckycollage_category_ids_map,$data['cid']);
					if($data['freighttype'] == 0) $data['freighttype'] = 1;
					//if($data['commissionset'] == 1 || $data['commissionset'] == 2 || $data['commissionset'] == 3) $data['commissionset'] = 0;
					$id = Db::name('lucky_collage_product')->insertGetId($data);
					$gglist = Db::name('lucky_collage_guige')->where('aid',$fromid)->where('proid',$oldid)->select()->toArray();
					foreach($gglist as $gg){
						$gg['id'] = '';
						$gg['aid'] = $toid;
						$gg['proid'] = $id;
						Db::name('lucky_collage_guige')->insert($gg);
					}
				}
			}
			if($modulename == '短视频'){
				//分类
				if($delold == 1) Db::name('shortvideo_category')->where('aid',$toid)->where('bid',0)->delete();
				$fromdata = Db::name('shortvideo_category')->where('aid',$fromid)->where('bid',0)->select()->toArray();
				$shortvideo_category_ids_map = [];
				$shortvideo_category_ids_map['0'] = '0';
				foreach($fromdata as $data){
					$oldid = $data['id'];
					$data['id'] = '';
					$data['aid'] = $toid;
					$shortvideo_category_ids_map[$oldid] = Db::name('shortvideo_category')->insertGetId($data);
				}
				//商品
				if($delold == 1) Db::name('shortvideo')->where('aid',$toid)->where('bid',0)->delete();
				$fromdata = Db::name('shortvideo')->where('aid',$fromid)->where('bid',0)->select()->toArray();
				foreach($fromdata as $data){
					$oldid = $data['id'];
					$data['id'] = '';
					$data['aid'] = $toid;
					$data['mid'] = 0;
					$data['cid'] = $this->getnewids($shortvideo_category_ids_map,$data['cid']);
					$id = Db::name('shortvideo')->insertGetId($data);
				}
			}
			if($modulename == '文章列表'){
				//分类
				if($delold == 1) Db::name('article_category')->where('aid',$toid)->where('bid',0)->delete();
				$fromdata = Db::name('article_category')->where('aid',$fromid)->where('bid',0)->select()->toArray();
				$article_category_ids_map = [];
				$article_category_ids_map['0'] = '0';
				foreach($fromdata as $data){
					$oldid = $data['id'];
					$data['id'] = '';
					$data['aid'] = $toid;
					$data['pid'] = $article_category_ids_map[$data['pid']];
					$article_category_ids_map[$oldid] = Db::name('article_category')->insertGetId($data);
				}
				//文章
				if($delold == 1) Db::name('article')->where('aid',$toid)->where('bid',0)->delete();
				$fromdata = Db::name('article')->where('aid',$fromid)->where('bid',0)->select()->toArray();
				foreach($fromdata as $data){
					$oldid = $data['id'];
					$data['id'] = '';
					$data['aid'] = $toid;
					$data['cid'] = $this->getnewids($article_category_ids_map,$data['cid']);
					$id = Db::name('article')->insertGetId($data);
				}
			}
			if($modulename == '用户论坛'){
				//分类
				if($delold == 1) Db::name('luntan_category')->where('aid',$toid)->delete();
				$fromdata = Db::name('luntan_category')->where('aid',$fromid)->select()->toArray();
				$luntan_category_ids_map = [];
				$luntan_category_ids_map['0'] = '0';
				foreach($fromdata as $data){
					$oldid = $data['id'];
					$data['id'] = '';
					$data['aid'] = $toid;
					$data['pid'] = $luntan_category_ids_map[$data['pid']];
					$luntan_category_ids_map[$oldid] = Db::name('luntan_category')->insertGetId($data);
				}
				//帖子
				if($delold == 1) Db::name('luntan')->where('aid',$toid)->delete();
				$fromdata = Db::name('luntan')->where('aid',$fromid)->select()->toArray();
				foreach($fromdata as $data){
					$oldid = $data['id'];
					$data['id'] = '';
					$data['aid'] = $toid;
					$data['cid'] = $this->getnewids($luntan_category_ids_map,$data['cid']);
					$id = Db::name('luntan')->insertGetId($data);
				}
			}
			if($modulename == '预约服务商品'){
				//分类
				if($delold == 1) Db::name('yuyue_category')->where('aid',$toid)->where('bid',0)->delete();
				$fromdata = Db::name('yuyue_category')->where('aid',$fromid)->where('bid',0)->select()->toArray();
				$yuyue_category_ids_map = [];
				$yuyue_category_ids_map['0'] = '0';
				foreach($fromdata as $data){
					$oldid = $data['id'];
					$data['id'] = '';
					$data['aid'] = $toid;
					$data['pid'] = $yuyue_category_ids_map[$data['pid']];
					$yuyue_category_ids_map[$oldid] = Db::name('yuyue_category')->insertGetId($data);
				}
				//服务
				if($delold == 1) Db::name('yuyue_fuwu')->where('aid',$toid)->where('bid',0)->delete();
				$fromdata = Db::name('yuyue_fuwu')->where('aid',$fromid)->where('bid',0)->select()->toArray();
				$yuyue_fuwu_ids_map = [];
				$yuyue_fuwu_ids_map['0'] = '0';
				foreach($fromdata as $data){
					$oldid = $data['id'];
					$data['id'] = '';
					$data['aid'] = $toid;
					$yuyue_fuwu_ids_map[$oldid] = Db::name('yuyue_fuwu')->insertGetId($data);
				}
				//商品
				if($delold == 1){
					$proids = Db::name('yuyue_product')->where('aid',$toid)->where('bid',0)->column('id');
					Db::name('yuyue_product')->where('aid',$toid)->where('bid',0)->delete();
					Db::name('yuyue_guige')->where('aid',$toid)->where('proid','in',$proids)->delete();
				}
				$fromdata = Db::name('yuyue_product')->where('aid',$fromid)->where('bid',0)->select()->toArray();
				foreach($fromdata as $data){
					$oldid = $data['id'];
					$data['id'] = '';
					$data['aid'] = $toid;
					$data['cid'] = $this->getnewids($yuyue_category_ids_map,$data['cid']);
					$data['fwid'] = $this->getnewids($yuyue_fuwu_ids_map,$data['fwid']);
					//if($data['commissionset'] == 1 || $data['commissionset'] == 2 || $data['commissionset'] == 3) $data['commissionset'] = 0;
					$id = Db::name('yuyue_product')->insertGetId($data);
					$gglist = Db::name('yuyue_guige')->where('aid',$fromid)->where('proid',$oldid)->select()->toArray();
					foreach($gglist as $gg){
						$gg['id'] = '';
						$gg['aid'] = $toid;
						$gg['proid'] = $id;
						Db::name('yuyue_guige')->insert($gg);
					}
				}
			}
			if($modulename == '知识付费课程'){
				//分类
				if($delold == 1) Db::name('kecheng_category')->where('aid',$toid)->where('bid',0)->delete();
				$fromdata = Db::name('kecheng_category')->where('aid',$fromid)->where('bid',0)->select()->toArray();
				$kecheng_category_ids_map = [];
				$kecheng_category_ids_map['0'] = '0';
				foreach($fromdata as $data){
					$oldid = $data['id'];
					$data['id'] = '';
					$data['aid'] = $toid;
					$data['pid'] = $kecheng_category_ids_map[$data['pid']];
					$kecheng_category_ids_map[$oldid] = Db::name('kecheng_category')->insertGetId($data);
				}
				$fromdata = Db::name('kecheng_list')->where('aid',$fromid)->where('bid',0)->select()->toArray();
				foreach($fromdata as $data){
					$oldid = $data['id'];
					$data['id'] = '';
					$data['aid'] = $toid;
					$data['cid'] = $this->getnewids($kecheng_category_ids_map,$data['cid']);
					$id = Db::name('kecheng_list')->insertGetId($data);
					$chapterlist = Db::name('kecheng_chapter')->where('aid',$fromid)->where('kcid',$oldid)->select()->toArray();
					foreach($chapterlist as $chapter){
						$chapter['id'] = '';
						$chapter['aid'] = $toid;
						$chapter['kcid'] = $id;
						Db::name('kecheng_chapter')->insert($chapter);
					}
					$tikulist = Db::name('kecheng_tiku')->where('aid',$fromid)->where('kcid',$oldid)->select()->toArray();
					foreach($tikulist as $tiku){
						$tiku['id'] = '';
						$tiku['aid'] = $toid;
						$tiku['kcid'] = $id;
						Db::name('kecheng_tiku')->insert($tiku);
					}
				}
			}
			if($modulename == '餐饮菜品'){
				//分类
				if($delold == 1) Db::name('restaurant_product_category')->where('aid',$toid)->where('bid',0)->delete();
				$fromdata = Db::name('restaurant_product_category')->where('aid',$fromid)->where('bid',0)->select()->toArray();
				$restaurant_category_ids_map = [];
				$restaurant_category_ids_map['0'] = '0';
				foreach($fromdata as $data){
					$oldid = $data['id'];
					$data['id'] = '';
					$data['aid'] = $toid;
					$data['pid'] = $restaurant_category_ids_map[$data['pid']];
					$restaurant_category_ids_map[$oldid] = Db::name('restaurant_product_category')->insertGetId($data);
				}
				//商品
				if($delold == 1){
					$proids = Db::name('restaurant_product')->where('aid',$toid)->where('bid',0)->column('id');
					Db::name('restaurant_product')->where('aid',$toid)->where('bid',0)->delete();
					Db::name('restaurant_product_guige')->where('aid',$toid)->where('product_id','in',$proids)->delete();
				}
				$fromdata = Db::name('restaurant_product')->where('aid',$fromid)->where('bid',0)->select()->toArray();
				foreach($fromdata as $data){
					$oldid = $data['id'];
					$data['id'] = '';
					$data['aid'] = $toid;
					$data['cid'] = $this->getnewids($restaurant_category_ids_map,$data['cid']);
					if($data['freighttype'] == 0) $data['freighttype'] = 1;
					//if($data['commissionset'] == 1 || $data['commissionset'] == 2 || $data['commissionset'] == 3) $data['commissionset'] = 0;
					$id = Db::name('restaurant_product')->insertGetId($data);
					$gglist = Db::name('restaurant_product_guige')->where('aid',$fromid)->where('product_id',$oldid)->select()->toArray();
					foreach($gglist as $gg){
						$gg['id'] = '';
						$gg['aid'] = $toid;
						$gg['product_id'] = $id;
						Db::name('restaurant_product_guige')->insert($gg);
					}
				}
			}
			if($modulename == '会员等级'){
				if($delold == 1){
					Db::name('member_level')->where('aid',$toid)->where('isdefault',0)->delete();
				}
				$fromdata = Db::name('member_level')->where('aid',$fromid)->select()->toArray();
				foreach($fromdata as $data){
					$oldid = $data['id'];
					unset($data['id']);
					$data['aid'] = $toid;
					if($data['isdefault'] == 1){
						Db::name('member_level')->where('aid',$toid)->where('isdefault',1)->update($data);
					}else{
						$id = Db::name('member_level')->insertGetId($data);
					}
				}
			}
			if($modulename == '优惠券'){
				if($delold == 1) Db::name('coupon')->where('aid',$toid)->where('bid',0)->delete();
				$fromdata = Db::name('coupon')->where('aid',$fromid)->where('bid',0)->select()->toArray();
				foreach($fromdata as $data){
					$oldid = $data['id'];
					$data['id'] = '';
					$data['aid'] = $toid;
					$id = Db::name('coupon')->insertGetId($data);
				}
			}
			if($modulename == '设计页面'){
				if($delold == 1){
					Db::name('designerpage')->where('aid',$toid)->where('bid',0)->delete();
				}else{
					Db::name('designerpage')->where('aid',$toid)->where('bid',0)->update(['ishome'=>0]);
				}
				$fromdata = Db::name('designerpage')->where('aid',$fromid)->where('bid',0)->select()->toArray();
				foreach($fromdata as $data){
					$oldid = $data['id'];
					$data['id'] = '';
					$data['aid'] = $toid;
					$id = Db::name('designerpage')->insertGetId($data);
				}
				$fromdata = Db::name('designerpage_tab')->where('aid',$fromid)->select()->toArray();
				foreach($fromdata as $data){
					$oldid = $data['id'];
					$data['id'] = '';
					$data['aid'] = $toid;
					$id = Db::name('designerpage_tab')->insertGetId($data);
				}
			}
		}
		return json(['status'=>1,'msg'=>'复制完成']);
	}
	private function getnewids($arr,$ids){
		if(!$ids) return $ids;
		$ids = explode(',',$ids);
		$newids = [];
		foreach($ids as $id){
			$newids[] = $arr[$id];
		}
		return implode(',',$newids);
	}
}
